package api

import (
	"context"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"service_base_shop_api/goods_web/global"
	protopb "service_base_shop_api/goods_web/proto"
	"service_base_shop_api/goods_web/utils"
	"strconv"
)

func GetIndex(ctx *gin.Context)  {
	list,err := global.GoodsSrvClient.GetIndexInfo(context.Background(),&protopb.GetIndexInfoReq{})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"brand":list.Brand,"ggtop":list.Ggtop,"course":list.Course,"procat":list.Procat,"prolist":list.Prolist})
}
func GetIndexList(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"page"})
	if parms == nil {
		return
	}
	page,_ := strconv.Atoi(parms["page"])

	list,err := global.GoodsSrvClient.GetIndexList(context.Background(),&protopb.GetIndexListReq{Page: int64(page)})
	if err != nil {
		ctx.JSON(http.StatusOK,gin.H{"prolist":[]string{}})
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"prolist":list.List,"status":1})

}



func GetGoodsIndex(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"pro_id"})
	if parms == nil {
		return
	}
	pro_id,_ := strconv.Atoi(parms["pro_id"])

	list,err := global.GoodsSrvClient.GetProductInfo(context.Background(),&protopb.GetProductReq{ProId:int64(pro_id) })
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	pro := map[string]interface{}{}
	attrValueList := []map[string]interface{}{}
	commodityAttr := []map[string]interface{}{}
	json.Unmarshal([]byte(list.ProInfo),&pro)
	json.Unmarshal([]byte(list.AttrValueList),&attrValueList)
	json.Unmarshal([]byte(list.CommodityAttr),&commodityAttr)
	ctx.JSON(http.StatusOK,gin.H{"pro":pro,"attrValueList":attrValueList,"commodityAttr":commodityAttr,"status":1})
}



/**
	获取分类
 */
func GetCategory(ctx *gin.Context)  {
	list,err := global.GoodsSrvClient.CateGoryIndex(context.Background(),&protopb.CateGoryReq{})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"catList":list.CatList,"list":list.List,"status":1})
}

func GetCate(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"cat_id"})
	if parms == nil {
		return
	}
	cat_id,_ := strconv.Atoi(parms["cat_id"])
	list,err := global.GoodsSrvClient.GetGatGoryByCatId(context.Background(),&protopb.GetGatGoryByCatIdReq{CatId: int64(cat_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"catList":list.List,"status":1})
}


func GetProList(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"cat_id","brand_id","ptype"})
	if parms == nil {
		return
	}
	keyword := ctx.DefaultPostForm("keyword","")
	post_page := ctx.DefaultPostForm("page","0")
	cat_id,_ := strconv.Atoi(parms["cat_id"])
	brand_id,_ := strconv.Atoi(parms["brand_id"])
	page,_ := strconv.Atoi(post_page)
	list,err := global.GoodsSrvClient.GetGatGoryList(context.Background(),&protopb.GetGatGoryListReq{Page: int64(page),CatId: int64(cat_id),Keyword: keyword,BrandId: int64(brand_id),Ptype: parms["ptype"]})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"pro":list.List,"cat_name":list.CatName,"status":1})
}